package com.bbzn.device.client.service.impl;

import com.bbzn.device.client.dao.planleader.PlanLeaderMapper;
import com.bbzn.device.client.dto.index.PlanDTO;
import com.bbzn.device.client.dto.index.PlanInfoDTO;
import com.bbzn.device.client.utils.BabaBeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.bbzn.device.client.dataobject.WorkPlan;
import com.bbzn.device.client.dao.workplan.WorkPlanMapper;
import com.bbzn.device.client.service.WorkPlanService;

@Service
public class WorkPlanServiceImpl implements WorkPlanService {

    @Resource
    private WorkPlanMapper workPlanMapper;

    @Resource
    private PlanLeaderMapper planLeaderMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return workPlanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(WorkPlan record) {
        return workPlanMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(WorkPlan record) {
        return workPlanMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(WorkPlan record) {
        return workPlanMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(WorkPlan record) {
        return workPlanMapper.insertSelective(record);
    }

    @Override
    public WorkPlan selectByPrimaryKey(Long id) {
        return workPlanMapper.selectByPrimaryKey(id);
    }

    @Override
    public int selectCountPrimaryKey(Long id) {
        return workPlanMapper.selectCountPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WorkPlan record) {
        return workPlanMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(WorkPlan record) {
        return workPlanMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<WorkPlan> findByAll(WorkPlan workPlan) {
        return workPlanMapper.findByAll(workPlan);
    }

    @Override
    public List<PlanDTO> findAllPlanByUserId(Long userId) {
        WorkPlan WorkPlan = new WorkPlan();
        WorkPlan.setUserId(userId);
        List<WorkPlan> workPlanList = workPlanMapper.findByAll(WorkPlan);//从缓存或数据库中查询全部
        //JDK8的stream处理,把根分类区分出来
        List<PlanDTO> roots = workPlanList.stream().filter(plan -> (plan.getPid() == 0)).
                map(p -> {
                    PlanDTO node = BabaBeanUtils.voToDto(p, PlanDTO.class);
                    return node;
                }).collect(Collectors.toList());
        //对根分类进行排序
        roots.sort(new Comparator<PlanDTO>() {
            @Override
            public int compare(PlanDTO o1, PlanDTO o2) {
                return o1.getId() > o2.getId() ? 1 : -1;
            }
        });

        //把非根分类区分出来
        List<PlanDTO> subs = workPlanList.stream().filter(p -> (p.getPid() != 0)).map(p -> {
            PlanDTO subNode = BabaBeanUtils.voToDto(p, PlanDTO.class);
            return subNode;
        }).collect(Collectors.toList());

        //递归构建结构化的分类信息
        roots.forEach(root -> buildSubs(root, subs));
        return roots;
    }

    @Override
    public List<PlanInfoDTO> findPlanInfoByUserId(Long userId) {
        return workPlanMapper.findPlanInfoByUserId(userId);
    }

    @Override
    public int delPlanById(Long userId, Long planId) {
        WorkPlan WorkPlan = new WorkPlan();
        WorkPlan.setUserId(userId);
        List<WorkPlan> workPlans = workPlanMapper.findByAll(WorkPlan);
        WorkPlan.setId(planId);
        List<Long> subPlanIds = new ArrayList<>();
        buildPlan(WorkPlan, subPlanIds, workPlans);
        subPlanIds.add(planId);
        planLeaderMapper.batchDelByPlanIds(subPlanIds);
        return workPlanMapper.batchDel(subPlanIds);
    }

    @Override
    public int batchDel(List<Long> list) {
        return 0;
    }

    /**
     * 递归构建
     *
     * @param parent
     * @param subs
     */
    private void buildSubs(PlanDTO parent, List<PlanDTO> subs) {
        List<PlanDTO> children = subs.stream().filter(sub -> (sub.getPid().equals(parent.getId()))).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(children)) {//有子分类的情况
            parent.setChildren(children);
            children.forEach(child -> buildSubs(child, subs));//再次递归构建
        }
    }

    /**
     * 递归构建
     */
    private void buildPlan(WorkPlan needPlan, List<Long> subPlanIds, List<WorkPlan> workPlans) {
        List<WorkPlan> children = workPlans.stream().filter(plan -> (needPlan.getId().equals(plan.getPid()))).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(children)) {//有子分类的情况
            subPlanIds.addAll(children.stream().map(WorkPlan::getId).collect(Collectors.toList()));
            children.forEach(child -> buildPlan(child, subPlanIds, workPlans));//再次递归构建
        }
    }

    @Override
    public int updateBatch(List<WorkPlan> list) {
        return workPlanMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<WorkPlan> list) {
        return workPlanMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<WorkPlan> list) {
        return workPlanMapper.batchInsert(list);
    }

}

